<template>
    <div class="post-lists">
        <!--    <el-card class="!border-none" shadow="never">-->
        <!--      <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">-->
        <!--        &lt;!&ndash;        <el-form-item label="名称">&ndash;&gt;-->
        <!--        &lt;!&ndash;          <el-input&ndash;&gt;-->
        <!--        &lt;!&ndash;            class="w-[280px]"&ndash;&gt;-->
        <!--        &lt;!&ndash;            v-model="queryParams.name"&ndash;&gt;-->
        <!--        &lt;!&ndash;            clearable&ndash;&gt;-->
        <!--        &lt;!&ndash;            @keyup.enter="resetPage"&ndash;&gt;-->
        <!--        &lt;!&ndash;          />&ndash;&gt;-->
        <!--        &lt;!&ndash;        </el-form-item>&ndash;&gt;-->
        <!--        <el-form-item label="逾期类型">-->
        <!--          <el-select clearable class="!w-[280px]" v-model="queryParams.type">-->
        <!--            <el-option v-for="(item, key) in typeList" :key="key" :label="item" :value="key" />-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item>-->
        <!--          <el-button type="primary" @click="resetPage">查询</el-button>-->
        <!--          <el-button @click="resetParams">重置</el-button>-->
        <!--        </el-form-item>-->
        <!--      </el-form>-->
        <!--    </el-card>-->

        <el-card class="!border-none" shadow="never">
            <common-table rowKey="key" :columns="columns" :pager="pager">
                <template #btns>
                    <div class="">
                        <el-button
                            v-perms="['config.classify/add']"
                            type="primary"
                            @click="handleAdd()"
                        >
                            <template #icon>
                                <icon name="el-icon-Plus" />
                            </template>
                            新增
                        </el-button>
                    </div>
                </template>
                <template #actions="{ row }">
                    <el-button
                        v-perms="['config.classify/edit']"
                        type="primary"
                        @click="handleEdit(row)"
                    >
                        编辑
                    </el-button>
                    <el-button
                        v-perms="['config.classify/delete']"
                        type="danger"
                        @click="handleDelete(row)"
                    >
                        删除
                    </el-button>
                </template>
            </common-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup name="OperationConfigClassifyLists">
import type { ColumnsType } from '@/components/common-table/types'
import feedback from '@/utils/feedback'

import EditPopup from './edit.vue'
import {
    apiConfigCateDelete,
    apiConfigCateTwoDelete,
    apiConfigCateTwoGetTreeAll
} from '@/api/config.ts'

const columns = reactive<ColumnsType[]>([
    {
        label: '名称',
        prop: 'name',
        minWidth: '180'
    },
    {
        label: '排序',
        prop: 'sort',
        minWidth: '180'
    },
    {
        label: '添加时间',
        prop: 'ctime',
        type: 'date',
        fmt: 'yyyy-mm-dd hh:MM:ss',
        minWidth: '200'
    }
])

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    name: '',
    type: ''
})

const pager = reactive({
    lists: [],
    loading: false
})

const getLists = async () => {
    pager.loading = true
    pager.lists = await apiConfigCateTwoGetTreeAll()
    pager.loading = false
}

// const { pager, getLists, resetPage, resetParams } = usePaging({
//     fetchFun: apiConfigCateTwoGetTreeAll,
//     params: queryParams
// })

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (row: any) => {
    await feedback.confirm('确定要删除？')
    if (row.cate_id) {
        await apiConfigCateTwoDelete({ id: row.id })
    } else {
        await apiConfigCateDelete({ id: row.id })
    }
    getLists()
}

onActivated(() => {
    getLists()
})
</script>
